import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait #强制等待的方法
from selenium.webdriver.support import expected_conditions as EC    #如果找不到就抛出异常
driver = webdriver.Chrome()   #实例化驱动（模板）
driver.get("http://www.bjhospital.net/")  #打开网页（前面为模板，后面为需要打开的网页）
driver.maximize_window()                 #全屏化
driver.implicitly_wait(15)             #设置隐式等待（15s）
#输入框输入“骨科”
# driver.find_element(By.CLASS_NAME,"keyword ").send_keys("骨科")
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.CLASS_NAME,"keyword"))).send_keys("骨科")
time.sleep(2)
#点击搜索按钮
# driver.find_elements(By.CLASS_NAME,"btn")[0].click()
WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"btn")))[0].click()
time.sleep(5)
#点击第一个链接
# driver.find_element(By.XPATH,"//*[text()='毕节市第一人民医院高值医用耗材网上阳光平台议价意向公开']").click()
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='毕节市第一人民医院高值医用耗材网上阳光平台议价意向公开']"))).click()
time.sleep(2)
#切换句柄
win = driver.window_handles #先获取所有句柄
driver.switch_to.window(win[-1])    #访问最后一个打开的句柄
#断言“您的位置：”
text = driver.find_element(By.XPATH,"//*[@id='nav']/ul/li[1]/a").text
assert text == "首页"
time.sleep(5)